/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            _this.i = 0;
            $.ajax({
                url: _global.url.api + 'tile/' + id,
                type: 'get',
                async: false,
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('data', res.data);
                    }
                }
            });
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            if (this.data.info) {
                $.each(this.data.info, function (i, info) {
                    var html = template('add-tpl', info);
                    $("#main-view").append(html);
                    _this.i++;
                });
            } else {
                var html = template('add-tpl', _this);
                $("#main-view").append(html);
            }

        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $("#add_table").on("click", function (e) {
                e.preventDefault();
                _this.i++;
                var html = template('add-tpl', _this);
                $("#main-view").append(html);
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_table").hide();
                    return false;
                }
                $(".delete_table").show();
            });
            $('#btn_save').on('click', function (e) {
                _this.data = {};
                _this.data.id = id;
                var titles = new Array();
                $("input[name='title']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        titles.push($(this).val());
                    }
                });
                _this.data.titles = titles.join(",");
                if (_this.data.titles == "") {
                    Alert({msg: '请输入活动标题', type: 'error'});
                    return false;
                }
                var imgs = new Array();
                $("input[name='imgs']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        imgs.push($(this).val());
                    }
                });
                _this.data.imgs = imgs.join(",");
                if (_this.data.imgs == "") {
                    Alert({msg: '请上传图片', type: 'error'});
                    return false;
                }
                var urls = new Array();
                $("input[name='url']").each(function () {
                    urls.push($.trim($(this).val()));

                });
                _this.data.urls = urls.join(",");
                var width = new Array();
                $("input[name='width']").each(function () {
                    width.push($.trim($(this).val()));

                });
                for (var i = 0; i < width.length; i++) {
                    if (!width[i]) {
                        Alert({msg: '请填写图片的宽度', type: 'error'});
                        return false;
                    }
                }
                _this.data.width = width.join(",");
                var height = new Array();
                $("input[name='height']").each(function () {
                    height.push($.trim($(this).val()));

                });
                for (var i = 0; i < height.length; i++) {
                    if (!height[i]) {
                        Alert({msg: '请填写图片的高度', type: 'error'});
                        return false;
                    }
                }
                _this.data.height = height.join(",");
                var native = new Array();
                $(".native").each(function () {
                    native.push($(this).val());
                });
                _this.data.native = native.join(",");
                var proportion = new Array();
                $("input[name='proportion']").each(function () {
                    proportion.push($(this).val());
                });
                _this.data.proportion = proportion.join(",");
                var intros = new Array();
                $("input[name='intros']").each(function () {
                    intros.push($(this).val());
                });
                _this.data.intros = intros.join(",");
                var check = true;
                for (var i = 0; i < urls.length; i++) {
                    if (native[i] == 'h5') {
                        if (!urls[i]) {
                            Alert({msg: 'h5活动页url必填', type: 'error'});
                            check = false;
                            return false;
                        }
                    }
                }
                if (!check) {
                    return false;
                }
                if(type==8){
                    if(imgs.length<5){
                        Alert({msg: '图片导航的图片不能小于5张', type: 'error'});
                        return false;
                    }
                }
                _this.data.listorder = $("#listorder").val();
                console.log(_this.data);
                if (new Model({
                        url: _global.url.api + 'tile',
                        data: _this.data
                    }).put()) {
                }
            });
            $('body').on('click', '.delete_table', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num" + item).remove();
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_table").hide();
                    return false;
                }
                $(".add_table").show();
            });
            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });


        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        }
    };

    new index();

});